{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-02-11T07:46:56.146667Z",
     "start_time": "2025-02-11T07:46:55.629242Z"
    }
   },
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "dict_data = {'A': 1,\n",
    "             'B': pd.Timestamp('20190926'),\n",
    "             'C': pd.Series(1, index=list(range(4)),dtype='float32'),\n",
    "             'D': np.array([1,2,3,4],dtype='int32'),\n",
    "             'E': [\"Python\",\"Java\",\"C++\",\"C\"],\n",
    "             'F': 'wangdao' }\n",
    "df_obj2 = pd.DataFrame(dict_data)\n",
    "print(df_obj2.index)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index([0, 1, 2, 3], dtype='int64')\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "#  常见的Index种类\n",
    "•Index，索引  可以是各种类型\n",
    "•Int64Index，整数索引\n",
    "•MultiIndex，层级索引，难度较大\n",
    "•DatetimeIndex，时间戳类型"
   ],
   "id": "af167efc8e823832"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T02:52:00.340402Z",
     "start_time": "2025-02-12T02:52:00.332063Z"
    }
   },
   "cell_type": "code",
   "source": [
    "ser_obj = pd.Series(range(5), index = list(\"abcde\"))\n",
    "print(ser_obj)\n",
    "print('--'*50)\n",
    "\n",
    "print(type(ser_obj.index))\n",
    "ser_obj.index"
   ],
   "id": "7a2b464fcc824d05",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n",
      "----------------------------------------------------------------------------------------------------\n",
      "<class 'pandas.core.indexes.base.Index'>\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Index(['a', 'b', 'c', 'd', 'e'], dtype='object')"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T02:52:21.863760Z",
     "start_time": "2025-02-12T02:52:21.848981Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(ser_obj.loc['b']) #索引名\n",
    "print(ser_obj.iloc[2]) #位置索引"
   ],
   "id": "e9d42c5ef4668945",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T02:52:33.458278Z",
     "start_time": "2025-02-12T02:52:33.435916Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 切片索引\n",
    "print(ser_obj.iloc[1:3])  #索引位置取数据，左闭右开\n",
    "print(ser_obj.loc['b':'d'])  #记住索引名  左闭右闭"
   ],
   "id": "1cb3d4fa61389910",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b    1\n",
      "c    2\n",
      "dtype: int64\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T02:52:42.056279Z",
     "start_time": "2025-02-12T02:52:42.030582Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 不连续索引\n",
    "print(ser_obj.iloc[[0, 2, 4]])\n",
    "print(ser_obj.loc[['a', 'e']])"
   ],
   "id": "c32e1940f8c87bdf",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0\n",
      "c    2\n",
      "e    4\n",
      "dtype: int64\n",
      "a    0\n",
      "e    4\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T02:53:08.579006Z",
     "start_time": "2025-02-12T02:53:08.570725Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 布尔索引\n",
    "ser_bool = ser_obj > 2\n",
    "print(ser_obj)\n",
    "print(ser_bool)\n"
   ],
   "id": "2755a09d013a47ca",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n",
      "a    False\n",
      "b    False\n",
      "c    False\n",
      "d     True\n",
      "e     True\n",
      "dtype: bool\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------------------------------------\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 12,
   "source": [
    "print('-'*50)\n",
    "print(ser_obj[ser_bool])\n",
    "\n",
    "print(ser_obj[ser_obj > 2]) #取出大于2的元素"
   ],
   "id": "1ce53bc7212e1fe2"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "#DataFrame索引",
   "id": "7b36588cab106b46"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T02:55:23.716975Z",
     "start_time": "2025-02-12T02:55:23.690946Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "df_obj = pd.DataFrame(np.random.randn(5,4),\n",
    "                      columns = ['a', 'b', 'c', 'd'])\n",
    "print(df_obj.head())"
   ],
   "id": "877edb93dfb6fa90",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          a         b         c         d\n",
      "0 -0.457779  1.640323 -0.104306 -0.648481\n",
      "1  0.635183 -1.129458 -0.281431  0.528827\n",
      "2  0.342570  0.807933  0.819939  0.428099\n",
      "3 -0.243315 -0.833796  0.980273 -1.676768\n",
      "4  0.324072 -0.501192 -1.401721  1.615009\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T02:56:14.566902Z",
     "start_time": "2025-02-12T02:56:14.554727Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 列索引\n",
    "print(df_obj['a']) # 返回Series类型\n",
    "print('-'*50)\n",
    "print(df_obj[['a']]) # 返回DataFrame类型\n",
    "print('-'*50)\n",
    "print(type(df_obj[['a']])) # 返回DataFrame类型"
   ],
   "id": "29fb8d16d8da86a6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0   -0.457779\n",
      "1    0.635183\n",
      "2    0.342570\n",
      "3   -0.243315\n",
      "4    0.324072\n",
      "Name: a, dtype: float64\n",
      "--------------------------------------------------\n",
      "          a\n",
      "0 -0.457779\n",
      "1  0.635183\n",
      "2  0.342570\n",
      "3 -0.243315\n",
      "4  0.324072\n",
      "--------------------------------------------------\n",
      "<class 'pandas.core.frame.DataFrame'>\n"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T02:59:26.381134Z",
     "start_time": "2025-02-12T02:59:26.373852Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 标签索引 loc，建议使用loc，效率更高\n",
    "# Series\n",
    "print(ser_obj)\n",
    "print(ser_obj['b':'d'])\n",
    "print(ser_obj.loc['b':'d']) #前闭后闭\n",
    "print('-'*50)\n"
   ],
   "id": "ff98f5354cc1053f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "dtype: int64\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "dtype: int64\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T03:00:23.563090Z",
     "start_time": "2025-02-12T03:00:23.554979Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# DataFrame\n",
    "df_obj = pd.DataFrame(np.random.randn(5,4),\n",
    "                      \n",
    "                      index=list('abcde'),columns = list('abcd'))\n",
    "print(df_obj)\n",
    "print('-'*50)\n",
    "print(df_obj['a'])  #建议不用,拿的是列\n",
    "print('-'*50)\n",
    "print(df_obj.loc['a'])  #拿的是行\n",
    "print('-'*50)\n"
   ],
   "id": "a98f7e7e0962223e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          a         b         c         d\n",
      "a -0.997562 -1.271690  0.738520 -0.061383\n",
      "b  0.380553 -1.455918 -0.770321 -1.162314\n",
      "c -0.012994 -0.397164 -1.536554 -2.055730\n",
      "d -0.115429 -0.415524  0.724596  0.634236\n",
      "e -0.473527 -0.253153  0.103876  0.129544\n",
      "--------------------------------------------------\n",
      "a   -0.997562\n",
      "b    0.380553\n",
      "c   -0.012994\n",
      "d   -0.115429\n",
      "e   -0.473527\n",
      "Name: a, dtype: float64\n",
      "--------------------------------------------------\n",
      "a   -0.997562\n",
      "b   -1.271690\n",
      "c    0.738520\n",
      "d   -0.061383\n",
      "Name: a, dtype: float64\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T03:01:15.720017Z",
     "start_time": "2025-02-12T03:01:15.709057Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 第一个参数索引行，第二个参数是列,loc或者iloc效率高于直接用取下标的方式，前闭后闭\n",
    "print(df_obj.loc['a':'c', 'b':'d']) #连续索引\n",
    "print('-'*50)\n",
    "print(df_obj.loc[['a','c'], ['b','d']]) #不连续索引\n",
    "print('-'*50)\n",
    "print(df_obj.loc[['c'],['b']]) #取一个值,返回的是DataFrame类型\n",
    "print('-'*50)\n",
    "print(df_obj.loc['c','b'])  #取一个值"
   ],
   "id": "968c8244e8baf520",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          b         c         d\n",
      "a -1.271690  0.738520 -0.061383\n",
      "b -1.455918 -0.770321 -1.162314\n",
      "c -0.397164 -1.536554 -2.055730\n",
      "--------------------------------------------------\n",
      "          b         d\n",
      "a -1.271690 -0.061383\n",
      "c -0.397164 -2.055730\n",
      "--------------------------------------------------\n",
      "          b\n",
      "c -0.397164\n",
      "--------------------------------------------------\n",
      "-0.39716416123307174\n"
     ]
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## iloc 位置索引(推荐使用)",
   "id": "9a000d76b3e30170"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T03:04:13.782450Z",
     "start_time": "2025-02-12T03:04:13.776270Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "print('-'*50)\n",
    "# Series\n",
    "print(ser_obj[1:3])\n",
    "print('-'*50)\n",
    "print(ser_obj.iloc[1:3]) # 前闭后开[)，效率高\n"
   ],
   "id": "2685266b9bb4a6e2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------------------------------------\n",
      "b    1\n",
      "c    2\n",
      "dtype: int64\n",
      "--------------------------------------------------\n",
      "b    1\n",
      "c    2\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T03:04:37.416659Z",
     "start_time": "2025-02-12T03:04:37.406758Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "# DataFrame，iloc是前闭后开[)\n",
    "print(df_obj)\n",
    "print('-'*50)\n",
    "print(df_obj.iloc[0:2, 0:2]) \n",
    "print('-'*50)\n",
    "print(df_obj.iloc[[0,2], [0,2]]) # 不连续索引\n",
    "print('-'*50)\n",
    "print(df_obj.iloc[0,0]) # 取一个值"
   ],
   "id": "482c59f6b04d3035",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          a         b         c         d\n",
      "a -0.997562 -1.271690  0.738520 -0.061383\n",
      "b  0.380553 -1.455918 -0.770321 -1.162314\n",
      "c -0.012994 -0.397164 -1.536554 -2.055730\n",
      "d -0.115429 -0.415524  0.724596  0.634236\n",
      "e -0.473527 -0.253153  0.103876  0.129544\n",
      "--------------------------------------------------\n",
      "          a         b\n",
      "a -0.997562 -1.271690\n",
      "b  0.380553 -1.455918\n",
      "--------------------------------------------------\n",
      "          a         c\n",
      "a -0.997562  0.738520\n",
      "c -0.012994 -1.536554\n",
      "--------------------------------------------------\n",
      "-0.9975624743361176\n"
     ]
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T03:05:20.404086Z",
     "start_time": "2025-02-12T03:05:20.391510Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#没有设置行和列索引的DataFrame，iloc和loc的区别\n",
    "df_obj2 = pd.DataFrame(np.random.randn(5,4))\n",
    "print(df_obj2)\n",
    "print('-'*50)\n",
    "print(df_obj2.iloc[0:2]) #左闭右开 2行\n",
    "print('-'*50)\n",
    "print(df_obj2.loc[0:2]) #左闭右闭 3行"
   ],
   "id": "83e07ff6ab51dda",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         1         2         3\n",
      "0 -1.303475 -1.457368  1.711966 -0.292723\n",
      "1 -0.221258  0.740380  1.258415 -0.326216\n",
      "2 -0.349790  0.726112 -1.215477 -0.386954\n",
      "3  1.943173  2.105999 -1.432376  2.077438\n",
      "4  0.837828  0.430780  1.236179  0.585906\n",
      "--------------------------------------------------\n",
      "          0         1         2         3\n",
      "0 -1.303475 -1.457368  1.711966 -0.292723\n",
      "1 -0.221258  0.740380  1.258415 -0.326216\n",
      "--------------------------------------------------\n",
      "          0         1         2         3\n",
      "0 -1.303475 -1.457368  1.711966 -0.292723\n",
      "1 -0.221258  0.740380  1.258415 -0.326216\n",
      "2 -0.349790  0.726112 -1.215477 -0.386954\n"
     ]
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "\n",
    "对齐运算"
   ],
   "id": "3c39cf4fbf47c777"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T03:05:55.666852Z",
     "start_time": "2025-02-12T03:05:55.646778Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "s1 = pd.Series(range(10, 20), index = range(10))\n",
    "s2 = pd.Series(range(20, 25), index = range(5))\n",
    "# Series 对齐运算\n",
    "print('s1+s2: ')\n",
    "s3=s1+s2\n",
    "print(s3)  #缺失数据默认是NaN  np.nan"
   ],
   "id": "3e1427da0a8e421f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "s1+s2: \n",
      "0    30.0\n",
      "1    32.0\n",
      "2    34.0\n",
      "3    36.0\n",
      "4    38.0\n",
      "5     NaN\n",
      "6     NaN\n",
      "7     NaN\n",
      "8     NaN\n",
      "9     NaN\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 24
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T03:06:49.874525Z",
     "start_time": "2025-02-12T03:06:49.867121Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#两个长度不同的一维ndarray相加\n",
    "a1 = np.array([1,2,3,4,5])\n",
    "a2 = np.array([1]) # 长度为1\n",
    "print(a2.shape)\n",
    "print(a1+a2)"
   ],
   "id": "dda1fc73e5746dcc",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1,)\n",
      "[2 3 4 5 6]\n"
     ]
    }
   ],
   "execution_count": 25
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T03:08:51.219153Z",
     "start_time": "2025-02-12T03:08:51.214760Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(s1)\n",
    "print(s2)"
   ],
   "id": "62109e07c2557069",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    10\n",
      "1    11\n",
      "2    12\n",
      "3    13\n",
      "4    14\n",
      "5    15\n",
      "6    16\n",
      "7    17\n",
      "8    18\n",
      "9    19\n",
      "dtype: int64\n",
      "0    20\n",
      "1    21\n",
      "2    22\n",
      "3    23\n",
      "4    24\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 26
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T03:15:13.311051Z",
     "start_time": "2025-02-12T03:15:13.305461Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(np.isnan(s3[6]))\n",
    "print('-'*50)\n",
    "print(s1.add(s2, fill_value = 0))  #未对齐的数据将和填充值做运算\n",
    "print(s1.sub(s2, fill_value = 0))"
   ],
   "id": "113025cb3dccb293",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "--------------------------------------------------\n",
      "0    30.0\n",
      "1    32.0\n",
      "2    34.0\n",
      "3    36.0\n",
      "4    38.0\n",
      "5    15.0\n",
      "6    16.0\n",
      "7    17.0\n",
      "8    18.0\n",
      "9    19.0\n",
      "dtype: float64\n",
      "0   -10.0\n",
      "1   -10.0\n",
      "2   -10.0\n",
      "3   -10.0\n",
      "4   -10.0\n",
      "5    15.0\n",
      "6    16.0\n",
      "7    17.0\n",
      "8    18.0\n",
      "9    19.0\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 30
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-12T03:16:23.090521Z",
     "start_time": "2025-02-12T03:16:23.078416Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#df的对齐运算\n",
    "import numpy as np\n",
    "df1 = pd.DataFrame(np.ones((2,2)), columns = ['a', 'b'])\n",
    "df2 = pd.DataFrame(np.ones((3,3)), columns = ['a', 'b', 'c'])\n",
    "print(df1)\n",
    "print(df2)\n",
    "print('-'*50)\n",
    "print(df2.dtypes)\n",
    "print(df1.dtypes)\n",
    "print('-'*50)\n",
    "print(df1-df2)\n",
    "print(df2.sub(df1, fill_value = 2)) #未对齐的数据将和填充值做运算"
   ],
   "id": "4c824663916a1152",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     a    b\n",
      "0  1.0  1.0\n",
      "1  1.0  1.0\n",
      "     a    b    c\n",
      "0  1.0  1.0  1.0\n",
      "1  1.0  1.0  1.0\n",
      "2  1.0  1.0  1.0\n",
      "--------------------------------------------------\n",
      "a    float64\n",
      "b    float64\n",
      "c    float64\n",
      "dtype: object\n",
      "a    float64\n",
      "b    float64\n",
      "dtype: object\n",
      "--------------------------------------------------\n",
      "     a    b   c\n",
      "0  0.0  0.0 NaN\n",
      "1  0.0  0.0 NaN\n",
      "2  NaN  NaN NaN\n",
      "     a    b    c\n",
      "0  0.0  0.0 -1.0\n",
      "1  0.0  0.0 -1.0\n",
      "2 -1.0 -1.0 -1.0\n"
     ]
    }
   ],
   "execution_count": 32
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "",
   "id": "a2869b91258dfef1"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 总结：没对齐的元素，默认填充NaN，对齐运算时，fill_value参数可以指定填充值。",
   "id": "f8a92ca626e33d26"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
